使用python+微信发送消息提醒,实现程序监控 您所在的位置:网站首页 python 给微信发信息 使用python+微信发送消息提醒,实现程序监控

使用python+微信发送消息提醒,实现程序监控

2024-06-18 22:24| 来源: 网络整理| 查看: 265

使用python+微信发送消息提醒,实现程序监控

使用python+微信可以非常方便的提醒自己运行的程序是否报错,监控程序运行状态

1.申请微信测试号

https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login

申请好后会得到 appID 和 appsecret ,之后会用到

userid 为关注公众号的用户id (扫码关注后随便发送一条消息)

template_id 为模板id(新增测试模板后会得到一个id)

模板如下所示,准备好上面的 appID ,appsecret ,userid, template_id 就可以开始运行程序了 image-20210203210939149

2.python程序

你可以不用复制直接从github上下载: https://github.com/yyh-001/sendwx

把要下载好的程序(sendwx.py)和要运行的python程序放在一个同一个文件夹内,直接开始第三步

你也可以新建一个python文件,将代码复制到文件,我这边保存为sendwx.py

import requests import json import os def get_token(appID,appsecret): url_token = 'https://api.weixin.qq.com/cgi-bin/token?' res = requests.get(url=url_token,params={ "grant_type": 'client_credential', 'appid':appID, 'secret':appsecret, }).json() # print(res) token = res.get('access_token') # print(res) return token #读取配置文件 def rdconfig(filename): if (os.path.exists(filename)): with open(filename,"r") as f: config = json.load(f) # print("读取配置文件完成...") return config else: print('初始化配置文件') with open(filename, "w") as f: appID = input('appID: ') appsecret = input('appsecret: ') userid = input('userid: ') template_id = input('template_id: ') token = get_token(appID, appsecret) res = {"appID":appID,"appsecret":appsecret,"token":token,"userid":userid,"template_id":template_id} res1 = json.dumps(res) f.write(str(res1)) f.close() return res #写入配置文件 def wrconfig(new_dict,filename): with open(filename, "w") as f: json.dump(new_dict, f) # print("写入配置文件完成...") def sendtext(token,userID,text): url_msg = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?' body = { "touser": userID, # 这里必须是关注公众号测试账号后的用户id "msgtype": "text", "text": { "content": text } } res = requests.post(url=url_msg, params={ 'access_token': token # 这里是我们上面获取到的token }, data=json.dumps(body, ensure_ascii=False).encode('utf-8')) return res def sendmb(token,template_id,userID,text,color): url_msg = 'https://api.weixin.qq.com/cgi-bin/message/template/send?' body = { "touser": userID, # 这里必须是关注公众号测试账号后的用户id "template_id": template_id, "url": "http://weixin.qq.com/download", "topcolor": color, "data": { "text":{ "value": text, "color": color } } } res = requests.post(url=url_msg, params={ 'access_token': token # 这里是我们上面获取到的token }, data=json.dumps(body, ensure_ascii=False).encode('utf-8')) return res def send(text): config = rdconfig('token.json') userID = config['userid'] template_id =config['template_id'] res = sendtext(config['token'],userID,text) if(res.json()['errcode']==42001): # token两小时过期后重新获取 config['token']=get_token(config['appID'],config['appsecret']) wrconfig(config,'token.json') res = sendtext(config['token'],userID,text) if(res.json()['errcode']==45047): # 客服消息如果长时间不回复将不能发,这边先换成模板消息 res = sendmb(config['token'],template_id,userID,text+'(请及时回复以免消息发送失败)','#FF0000') return res.json() print(send('程序开始')) # 发送模板消息 # sendmb(token,template_id,userID,'程序错误,请及时回复','#FF0000') 3.实现程序监控,发送消息提醒

运行程序,程序第一次执行会初始化配置文件,将之前获得的 appID ,appsecret ,userid, template_id依次填入控制台,配置文件会保存在本地的【token.json】中,第一次配置之后就不用管了,程序会自动更新token,只需要在别的程序导入这个包既可

from sendwx import send #我之前保存的是sendwx.py send('这里填入你要发送的消息即可')

【示例】程序报错监控

from sendwx import send #我之前保存的是sendwx.py try: #里面写你要监控的程序 except Exception as e: send('程序异常\n' + str(e))

如果微信接收不到可能是折叠在公众号内部,只需要打开微信-设置-新消息提醒-新消息系统通知-将其他通知的允许通知打开即可

最近在学django,可以的话弄一个部署到服务器会更加方便



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有